package com.sgb.kjwl.viewmodel.self.logistics;

import android.app.Application;
import android.content.Context;
import android.text.TextUtils;
import android.view.Gravity;

import androidx.annotation.NonNull;
import androidx.databinding.ObservableField;
import androidx.lifecycle.AndroidViewModel;
import androidx.lifecycle.MutableLiveData;

import com.sgb.kjwl.api.APIManager;
import com.sgb.kjwl.api.NewAPIManager;
import com.sgb.kjwl.model.entity.self.OrderQuoteEntity;
import com.sgb.kjwl.model.entity.transport.OwnerGoodsDetailInfo;
import com.sgb.kjwl.model.response.ListResponse2;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.dialog.DialogHelper;
import com.swgk.core.dialog.RoundDialog;
import com.swgk.core.util.MToast;

import org.jetbrains.annotations.NotNull;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * Describe:
 * Author:
 * Date:    2021/8/24
 */
public class OrderOwnerFragmentModel extends AndroidViewModel {
    private int page = 1;
    public MutableLiveData<OwnerGoodsDetailInfo> detailsLiveData = new MutableLiveData<>();
    public MutableLiveData<List<OrderQuoteEntity>> ldListOrder = new MutableLiveData<>();
    public ObservableField<Boolean> isMore = new ObservableField<>(false);
    public MutableLiveData<Boolean> agreeOfferRefresh = new MutableLiveData<>();

    public OrderOwnerFragmentModel(@NonNull @NotNull Application application) {
        super(application);
    }

    /**
     * 请求最新的车主接单列表信息
     *
     * @param isMore
     * @param detailInfo 货源详情
     */
    public void getOrderOwnerList(Context context, boolean isMore, OwnerGoodsDetailInfo detailInfo) {
        if (detailInfo == null)
            return;
        page = isMore ? page + 1 : 1;
        Map map = new HashMap();
        map.put("limit", 10);
        map.put("page", page);
        map.put("goodsSourceNo", detailInfo.getNo());
        map.put("isVoluntarily", 2);
        DialogHelper.showProgressDialog(context, null, "加载中", 0, false, null).setCanceledOnTouchOutside(false);
        NewAPIManager.getInstance().getLogisticsAPI().getownerQuotationPriceOrOrders(map).enqueue(new Callback<BaseEntity<ListResponse2<List<OrderQuoteEntity>>>>() {
            @Override
            public void onResponse(Call<BaseEntity<ListResponse2<List<OrderQuoteEntity>>>> call, Response<BaseEntity<ListResponse2<List<OrderQuoteEntity>>>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() != null && response.body().getState().equals("ok")) {
                    if (response.body().getData() != null && response.body().getData().getList() != null) {
                        ldListOrder.postValue(response.body().getData().getList());
                    }
                } else {
                    if (response.body() != null)
                        MToast.showToast(context, response.body().getMsg());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<ListResponse2<List<OrderQuoteEntity>>>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }

    /**
     * 同意车主接单
     *
     * @param id goodsOffer报价编号
     * @param no 货源编号
     */
    public void agreeOffer(Context context, String id, String no) {
        Map map = new HashMap();
        map.put("no", id);
        map.put("goodsSourceNo", no);
        DialogHelper.showProgressDialog(context, null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        NewAPIManager.getInstance().getLogisticsAPI().shipperAgreeOffer(map).enqueue(new Callback<BaseEntity>() {
            @Override
            public void onResponse(Call<BaseEntity> call, Response<BaseEntity> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null || response.body().getState() == null || response.body().getCode() == null) {
                    return;
                }
                //报价成功
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    agreeOfferRefresh.postValue(true);
                }
                //车辆状态异常
                else if (response.body().getState().equals(APIManager.RESPONSE_FAIL)
                        && response.body().getCode().equals(APIManager.CAR_ABNORMAL)) {
                    if (!TextUtils.isEmpty(response.body().getMsg())) {
                        new RoundDialog.Builder(context)
                                .title("车辆状态异常")
                                .content(response.body().getMsg())
                                .confirm("确定")
                                .singleBtn(true)
                                .contentGravity(Gravity.CENTER)
                                .cancelable(false)
                                .show();
                    }
                } else {
                    MToast.showToast(context, response.body().getMsg());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }
}
